package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.dto.AnesthesiaInfoDTO;
import com.hksj.Intelligentmanagement.dto.SurgicalInfoDTO;
import com.hksj.Intelligentmanagement.entity.AnesthesiaInfoEntity;
import com.hksj.Intelligentmanagement.entity.SurgicalInfoEntity;
import com.hksj.Intelligentmanagement.mapper.AnesthesiaInfoMapper;
import com.hksj.Intelligentmanagement.mapper.SurgicalInfoMapper;
import com.hksj.Intelligentmanagement.vo.AnesthesiaInfoVo;
import com.hksj.Intelligentmanagement.vo.SurgicalInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SurgicalInfoService
 * @Description TODO
 * @Author @陈章
 * @Date 2023/3/31 11:06
 * @Version 1.0
 */
@Service
public class SurgicalInfoService extends ServiceImpl<SurgicalInfoMapper, SurgicalInfoEntity> {

    @Autowired
    private SurgicalInfoMapper surgicalInfoMapper;
    @Autowired
    private AnesthesiaInfoMapper anesthesiaInfoMapper;

    public List<SurgicalInfoVo> selectAllSurgicalInfo(String time) {
        //查所有手术信息
        List<SurgicalInfoVo> list = surgicalInfoMapper.selectAllSurgicalInfo(time);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<SurgicalInfoVo>();
        }
        //过滤出所有的id
        List<String> ids = list.stream().map(SurgicalInfoVo::getId).collect(Collectors.toList());
        //通过手术信息的ids查询所有的麻醉/副麻的人员信息
        List<AnesthesiaInfoVo> vos = anesthesiaInfoMapper.selectAllAnesthesiaInfoByIds(ids);
        //重新装配对象
        for (SurgicalInfoVo surgicalInfoVo : list) {
            List<AnesthesiaInfoVo> result = vos.stream()
                    .filter(obj -> obj.getSurgicalInfoId().equals(surgicalInfoVo.getId()))
                    .collect(Collectors.toList());
            surgicalInfoVo.setList(result);
        }
        return list;
    }


    public void updateEstimateTime(String id, String startTime,String endTime) {
        int count=surgicalInfoMapper.updateEstimateTime(id,startTime,endTime);
        if (count<=0){
            throw new RuntimeException("编辑失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addSurgicalInfo(SurgicalInfoDTO dto) {
        SurgicalInfoEntity surgicalInfoEntity = new SurgicalInfoEntity();
        BeanUtils.copyProperties(dto, surgicalInfoEntity);
        int insert = surgicalInfoMapper.insert(surgicalInfoEntity);
        //新增医护协助人员
        List<AnesthesiaInfoDTO> list = dto.getList();
//        List<AnesthesiaInfoEntity> entities = list.stream()
//                .map(anesthesiaInfoDTO -> {
//                    AnesthesiaInfoEntity entity = new AnesthesiaInfoEntity();
//                    BeanUtils.copyProperties(anesthesiaInfoDTO, entity);
//                    return entity;
//                })
//                .collect(Collectors.toList());
//        entities.stream()
//                .peek(obj -> obj.setId(surgicalInfoEntity.getId()))
//                .collect(Collectors.toList());
        list.stream()
                .peek(obj -> obj.setSurgicalInfoId(surgicalInfoEntity.getId()))
                .collect(Collectors.toList());
        int j = anesthesiaInfoMapper.insetAnesthesiaInfoList(list);
        if (insert <= 0 || j <= 0) {
            throw new RuntimeException("新增失败");
        }

    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void delSurgicalInfo(String id) {
        int i = surgicalInfoMapper.deleteById(id);

        QueryWrapper<AnesthesiaInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("surgical_info_id", id);
        int delete = anesthesiaInfoMapper.delete(wrapper);
        if (i <= 0 || delete <= 0) {
            throw new RuntimeException("删除失败");
        }
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateSurgicalInfo(SurgicalInfoDTO dto) {
        SurgicalInfoEntity surgicalInfoEntity = new SurgicalInfoEntity();
        BeanUtils.copyProperties(dto, surgicalInfoEntity);
        int i = surgicalInfoMapper.updateById(surgicalInfoEntity);

        QueryWrapper<AnesthesiaInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("surgical_info_id", dto.getId());
        anesthesiaInfoMapper.delete(wrapper);

        List<AnesthesiaInfoDTO> list = dto.getList();
        list.stream()
                .peek(obj -> obj.setSurgicalInfoId(dto.getId()))
                .collect(Collectors.toList());
        int j = anesthesiaInfoMapper.insetAnesthesiaInfoList(list);
        if (i <= 0) {
            throw new RuntimeException("更新失败");
        }
    }

}
